<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="../plugins/select2/select2.css" />
    <link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
    <script src="../plugins/select2/select2.min.js" type="text/javascript"></script>
<script>
	$(function () {
		showTemplate();
		$("#brands").select2();//下拉框多选技术
		$("#specs").select2();
		$("#updateBrand").select2();
		$("#updateSpec").select2();

	})
	function showTemplate() {
			var name = $("#name").val();
			$.ajax({
				url:"/template/templateList",
				type:"post",
				dataType:"json",
				data:{name:name},
				success:function (result) {
                   var data = result.data;
                   var tbody="";
                   $(data).each(function (i,e) {
                    //	console.log("dddd"+e.name);
                   	//  console.log("-----"+i);
                   	//从后台传来规格 strng装成json
					   var specList = JSON.parse(e.specIds);
					//   console.log("///"+specList);
					//   console.log("///\\\"+e.specIds);
					   //品牌
					   var brandList = JSON.parse(e.brandIds);
					   //自定义
					   var customAttributeItemsList = JSON.parse(e.customAttributeItems);
					   tbody += '<tr>';
					   tbody += '<td><input type="checkbox" value="'+data[i].id+'"></td>';
					   tbody += '<td>'+data[i].id+'</td>';
					   tbody += '<td>'+data[i].name+'</td>';
					   tbody += '<td>';
					   $(specList).each(function (i,e) {
							if (i==specList.length-1){

								tbody +=e.text;
							}else {
								tbody +=e.text+",";
							}
					   })
					   tbody += '</td>';
					   tbody += '<td>'
					   $(brandList).each(function (i1,e1) {
						   if (i1==brandList.length-1){
							   tbody +=e1.text;
						   }else {
							   tbody += e1.text + ",";
						   }
					   })
					   tbody += '</td>';
					   tbody += '<td>'
					   $(customAttributeItemsList).each(function (i2,e2) {
						   if (i2==customAttributeItemsList.length-1){
							   tbody +=e2.text;
						   }else {
							   tbody += e2.text+",";
						   }
					   })
					   tbody += '</td>';
					   tbody += ' <td class="text-center">';
					   tbody += '<button type="button" onclick="getTempById('+data[i].id+')" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateditModal" >修改</button>';
					   tbody += '</td></tr>';
				   })
					$("#tbody").html(tbody);

				},
				error:function () {
					alert("查询失败");
				}
			})
	}

	//删除
	function deleteTemplates() {
		var boxs=$("[type=checkbox]:checked");
		var ids=[];
		$(boxs).each(function (i,e) {
			ids.push(e.value);
		//	console.log("..."+e.value);
		})
		if (ids.length <= 0){
			alert("请选择要删除的数据");
			return;
		}
		$.ajax({
			url:"/template/deleteTemplates",
			type:"post",
			dataType:"json",
			data:{ids:ids},
			success:function (result) {
				alert("删除成功");
				location.reload();
			},
			error:function () {
             alert("删除失败");
			}
		})
	}

	//增加table
	function insertTable() {
		var tbody = document.getElementById("insertTable");
		var tr = document.createElement("tr");
		tr.innerHTML='<td><input type="checkbox" class="icheckbox_square-blue" ></td>'+'<td><input class="form-control" placeholder="属性名称" ></td>'+
				'<td><button type="button" class="btn btn-default" title="删除" onclick="deleteId(this)"><i class="fa fa-trash-o"></i> 删除</button> </td>';
		//添加tr标签
		tbody.appendChild(tr);
	}
	//修改
	function updateTable() {
		var tbody = document.getElementById("updateTable");
		//创建tr
		var tr = document.createElement("tr");
		//tr样式
		tr.innerHTML='<td><input type="checkbox" class="icheckbox_square-blue" ></td>' + '<td><input class="form-control" placeholder="属性名称" ></td>'+
				'<td><button type="button" class="btn btn-default" title="删除" onclick="deleteId(this)"><i class="fa fa-trash-o"></i> 删除</button> </td>';
		//添加tr标签
		tbody.appendChild(tr);
	}
	//删除table
	function deleteId(object) {
		var obj=object.parentNode.parentNode;
		obj.parentNode.removeChild(obj);
	}

	//增加
	function insert() {
		var name = $("#name1").val();
		//关联品牌 下拉框多选  获取你选中的所有数据
		var brands = $("#brands").select2('data');
		var arr=[];
		$(brands).each(function (i1,e1) {
			var brand={id:e1.id,text:e1.text.trim()};
			arr.push(brand);
		})
		var str = JSON.stringify(arr);
		console.log(str);
		//关联模板
		var specs = $("#specs").select2('data');
		var arr1=[];
		$.each(specs,function (index,e) {
			var spec={id:e.id,text:e.text};
			arr1.push(spec);
		})
		var str1 = JSON.stringify(arr1);
		//自定义模式
		var customList=[];
		var trs = $("#insertTable").find("tr");
		for (var i=0;i<trs.length;i++){
			var tds = $(trs[i]).find("td");
			var custom = $(tds).eq(1).find("input").val();
			var customs={text:custom};
			customList.push(customs);
		}
		var json = JSON.stringify(customList);
		$.ajax({
			url:"/template/insertTemplate",
			type:"post",
			dataType:"json",
			data:{name:name,brandIds:str,specIds:str1,customAttributeItems:json},
			success:function (result) {
				alert(result.message)
				location.reload();
			},
			error:function () {
				alert("增加失败");
			}
		})
	}

	//回显
	function getTempById(id) {
	   $.ajax({
		   url:"/template/getTempById/"+id,
		   type:"post",
		   dataType:"json",
		   success:function (result) {
              $("#tempId").val(result.data.id);
              //console.log(result.data.id);
              $("#names").val(result.data.name);
              //品牌 将string类型转换成json
              var brandList = JSON.parse(result.data.brandIds);
              var specList = JSON.parse(result.data.specIds);
              var customList = JSON.parse(result.data.customAttributeItems);
              var brand=[];
              $(brandList).each(function (i,e) {
				  brand.push(e.id);
			  })
			   var spec=[];
              $(specList).each(function (i1,e1) {
					spec.push(e1.id)
			  })
			   //trigger() 方法触发被选元素的指定事件类型
			   //select2技术回显方法
			   $("#updateBrand").val(brand).trigger("change");
			   $("#updateSpec").val(spec).trigger("change");
			   var tbody="";
			   for (var j=0;j<customList.length;j++){
			   	tbody +='<tr>';
			   	tbody +='<td><input type="checkbox" class="icheckbox_square-blue" ></td>';
			   	tbody +='<td><input class="form-control" placeholder="属性名称" value="'+customList[j].text+'"></td>';
			   	tbody +='<td><button type="button" class="btn btn-default" title="删除" onclick="deleteId(this)"><i class="fa fa-trash-o"></i> 删除</button></td>';
			   	tbody +='</tr>';
			   }
			   $("#updateTable").html(tbody);
		   },
		   error:function () {
				alert("回显失败");
		   }
	   })
	}

	//修改
	function updateTemplate() {
		var id = $("#tempId").val();
		var name=$("#names").val();
		//品牌
		var brands = $("#updateBrand").select2('data');
		var brandList=[];
		$(brands).each(function (i,e) {
			var brand={id:e.id,text:e.text};
			brandList.push(brand);
		})
		var brandStr = JSON.stringify(brandList);
		//规格
		var specs = $("#updateSpec").select2('data');
		var specList=[];
		$(specs).each(function (i1,event) {
           var spec={id:event.id,text:event.text};
           specList.push(spec);
		})
		var specStr = JSON.stringify(specList);
		//自定义属性
		var customList=[];
		$("#updateTable").find("tr").each(function () {
           var tds = $(this).children();
           var text = tds.eq(1).find("input").val();
           var custom={text:text};
           customList.push(custom);
		})
		var customStr = JSON.stringify(customList);
		$.ajax({
			url:"/template/updateTemplate",
			type:"post",
			dataType:"json",
			data:{id:id,name:name,specIds:specStr,brandIds:brandStr,customAttributeItems:customStr},
			success:function (result) {
				alert("修改成功");
				location.reload();
			},
			error:function () {
				alert("修改失败");
			}
		})

	}
</script>
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteTemplates()"><i class="fa fa-trash-o"></i> 删除</button>
                                       
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                  分类模板名称：<input id="name" >
									<button class="btn btn-default" onclick="showTemplate()">查询</button>
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联品牌</th>
									      <th class="sorting">关联规格</th>									     
									      <th class="sorting">扩展属性</th>													     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="tbody">
			                        <!--  <tr>
			                              <td><input  type="checkbox"></td>			                              
				                          <td>1</td>
									      <td>手机</td>
									      <td>联想,华为,中兴</td>
									      <td>网络制式</td>									 
									      <td></td>		                                                                  
		                                  <td class="text-center">                                           
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           
		                                  </td>
			                          </tr>
									  <tr>
			                              <td><input  type="checkbox"></td>			                              
				                          <td>2</td>
									      <td>服装</td>
									      <td>七匹狼,雅戈尔</td>
									      <td>服装尺码</td>									 
									      <td></td>		                                                                  
		                                  <td class="text-center">                                           
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           
		                                  </td>
			                          </tr>-->
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                        
							  
							 
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
                    
	           
					    
                                
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品类型模板编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>商品类型</td>
		      		<td><input  class="form-control" id="name1" placeholder="商品类型" name="name">  </td>
		      	</tr>			   
		      	<tr>
		      		<td>关联品牌</td>
		      		<td>                   <!--mu支持多个-->
		      			<select id="brands" multiple style="width: 100%">
							<option th:each="brand : ${brandAll}" th:value="${brand.id}">[[${brand.name}]]</option>
						</select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td>
						<select id="specs" multiple style="width: 100%">
							<option th:each="spec : ${specAll}" th:value="${spec.id}">[[${spec.specName}]]</option>
						</select>
		      		</td>
		      	</tr>	
		      	
		      	<tr>
		      		<td>扩展属性</td>
		      		<td>
		      			<div class="btn-group">
                             <button type="button" onclick="insertTable()" class="btn btn-default" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>
                                                                    
                        </div>
						<table class="table table-bordered table-striped"  width="800px">
							<thead>
								<tr>
								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>									
									<td>操作</td>
								</tr>
							</thead>
							<tbody id="insertTable">
								<!--<tr>
									<td><input type="checkbox" class="icheckbox_square-blue" ></td>
									<td><input class="form-control" placeholder="属性名称" ></td>
									<td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>
								<tr>
									<td><input type="checkbox" class="icheckbox_square-blue" ></td>
									<td><input class="form-control" placeholder="属性名称" ></td>
									<td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>-->
							</tbody>
						</table>	
           
		      		</td>
		      	</tr>	
		      	      
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="insert()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
  <!-- 修改窗口 -->
  <div class="modal fade" id="updateditModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel">商品类型模板编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <input type="text" id="tempId" name="id">
					  </tr>
					  <tr>
						  <td>商品类型</td>
						  <td><input  class="form-control" placeholder="商品类型" name="name" id="names">  </td>
					  </tr>
					  <tr>
						  <td>关联品牌</td>
						  <td>
							  <select id="updateBrand" multiple style="width: 100%">
								  <option th:each="brand : ${brandAll}" th:value="${brand.id}">[[${brand.name}]]</option>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>关联规格</td>
						  <td>
							  <select id="updateSpec" multiple style="width: 100%">
								  <option th:each="spec : ${specAll}" th:value="${spec.id}">[[${spec.specName}]]</option>
							  </select>
						  </td>
					  </tr>

					  <tr>
						  <td>扩展属性</td>
						  <td>
							  <div class="btn-group">
								  <button type="button"  class="btn btn-default" onclick="updateTable()" title="修改扩展属性"><i class="fa fa-file-o"></i> 修改扩展属性</button>

							  </div>
							  <table class="table table-bordered table-striped"  width="800px">
								  <thead>
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue"></td>
									  <td>属性名称</td>
									  <td>操作</td>
								  </tr>
								  </thead>
								  <tbody id="updateTable">
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue" ></td>
									  <td><input class="form-control" placeholder="属性名称" ></td>
									  <td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								  </tr>
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue" ></td>
									  <td><input class="form-control" placeholder="属性名称" ></td>
									  <td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								  </tr>
								  </tbody>
							  </table>

						  </td>
					  </tr>

				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateTemplate()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
    
</body>

</html>